package com.chandra.pos.product.web;


import com.chandra.pos.common.web.POSController;
import com.chandra.pos.product.model.ProductFilter;
import com.chandra.pos.product.model.ProductList;
import com.chandra.pos.product.model.ProductModel;
import com.chandra.pos.product.service.ProductManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityNotFoundException;

/**
 * Created by Admin on 7/2/14.
 */
@Controller
@RequestMapping("product")
public class ProductController extends POSController {


    Logger logger = LoggerFactory.getLogger(ProductController.class);


    private static String PAGE_MODULE = "product";
    private static String PAGE_NEW_PRODUCT = "newproduct";

    @Autowired
    private ProductManager productManager;


    public ProductController() {
        this.setTabIndex(2);

    }

    @RequestMapping(value = "getProductDetail", method = RequestMethod.GET)
    public
    @ResponseBody
    ProductModel getProductDetail(ProductFilter filter) {

        ProductModel employeeModel = new ProductModel();
        try {

            employeeModel = productManager.getProductDetail(filter);
        } catch (EntityNotFoundException e) {

        }

        return employeeModel;
    }


    @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
    public String index(ProductModel model) {

        return getPageURL(PAGE_MODULE, PAGE_HOME);

    }

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String home(ProductModel model) {

        return getPageView(PAGE_MODULE, PAGE_HOME);

    }


    @RequestMapping(value = "search", method = RequestMethod.GET)
    public
    @ResponseBody
    ProductList search(ProductFilter filter) {


        return productManager.search(filter);


    }


    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String newProduct(ProductModel model) {

        return getPageView(PAGE_MODULE, PAGE_NEW_PRODUCT);

    }

    @RequestMapping(value = "save", method = RequestMethod.POST)
    public
    @ResponseBody
    ProductModel save(ProductModel productModel) {


        productModel = productManager.saveProduct(productModel);
        return productModel;

    }
}